package com.blog.controller;

import com.blog.common.ReturnResult;
import com.blog.model.ArticleInfo;
import com.blog.model.ArticleUser;
import com.blog.pojo.Article;
import com.blog.pojo.User;
import com.blog.service.ArticleService;
import com.blog.service.DiscussService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author jiafujun
 * 2020-05-13 10:17:09
 **/
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ReturnResult save(HttpServletRequest request, Article article) {
        User user = (User) request.getSession().getAttribute("user");
        article.setUserId(user.getId());
        articleService.save(article);
        return ReturnResult.ok();
    }

    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    public ReturnResult getById(@RequestParam(required = true) Integer articleId) {
        Article article = articleService.selectByPrimaryKey(articleId);
        return ReturnResult.ok(article);
    }


    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ReturnResult list(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        Article article = new Article();
        article.setUserId(user.getId());
        List<Article> list = articleService.getList(article);
        return ReturnResult.ok(list);
    }

    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
    public ReturnResult del(@PathVariable Integer id) {
        articleService.del(id);
        return ReturnResult.ok();
    }

    @RequestMapping(value = "/getData", method = RequestMethod.POST)
    public ReturnResult getData() {
        Map<String, Object> data = articleService.getData();
        return ReturnResult.ok(data);
    }

    /**
     * 获取文章详情
     * @param articleId
     * @return
     */
    @RequestMapping(value = "/getByInfo", method = RequestMethod.POST)
    public ReturnResult getByInfo(@RequestParam Integer articleId) {
        ArticleInfo info= articleService.selectById(articleId);
        return ReturnResult.ok(info);
    }

    @RequestMapping(value = "/browse", method = RequestMethod.POST)
    public ReturnResult browse(Integer articleId) {
        Article article = new Article();
        article.setId(articleId);
        article.setBrowseNum(1);
        articleService.browseRecord(article);
        return ReturnResult.ok();
    }

    @RequestMapping(value = "/praise", method = RequestMethod.POST)
    public ReturnResult praise(Integer articleId) {
        Article article = new Article();
        article.setId(articleId);
        article.setPraiseNum(1);
        articleService.update(article);
        return ReturnResult.ok();
    }

    @RequestMapping(value = "/about",method = RequestMethod.POST)
    public ReturnResult praise(){
        ArticleInfo info= articleService.selectById(0);
        return ReturnResult.ok(info);
    }
}
